package com.zhang.learn.thymeleaf.controller;

import com.zhang.learn.thymeleaf.vo.School;
import com.zhang.learn.thymeleaf.vo.Student;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

@Controller
@Slf4j
@Tag(name = "thymeleaf测试")
public class ThymeleafTestController {
    @Autowired
    private MessageSource messageSource;

    @RequestMapping(path = "/test",method = RequestMethod.GET)
    public String test(){
        log.error("test");
        return "test";
    }

    /**
     * 使用Model对象来存放返回的数据，存放到Request中
     * @param model
     * @return
     */
    @Operation()
    @RequestMapping(path = "/test02",method = RequestMethod.GET)
    public String test02(Model model) {
        // 基础属性
        model.addAttribute("name","zhangsan");

        // 对象属性
        Student student = new Student();
        student.setName("zhangsan");
        student.setAge(20);
        student.setEmail("aaa@email.com");
        student.setSchool(new School("清华大学","北京市海淀区"));

        model.addAttribute("student",student);
        return "test02";
    }

    /**
     * 测试thymeleaf链接表达式的语法
     * 获取test03页面
     * @return
     */
    @RequestMapping(path = "test03",method = RequestMethod.GET)
    public String test03(Model model) {
        model.addAttribute("myName","https://bilibili.com");
        return "test03";
    }

    /**
     * 测试thymeleaf消息表达式语法，以及国际化的使用
     * @param model
     * @return
     */
    @RequestMapping(path = "test04",method = RequestMethod.GET)
    public String test04(Model model, HttpServletRequest request) {
        // 根据请求头判断语言
        // String language = request.getHeader("language");
        // Locale locale = language == null ? Locale.getDefault() : Locale.forLanguageTag(language);
        // log.error("greeting = " + messageSource.getMessage("greeting", null, locale));
        // model.addAttribute("myName","https://bilibili.com");

        // 可以通过语言和国家来获取对应的语言
        // Locale l1 = new Locale("zh","CN");

        return "test04";
    }

    /**
     * 测试thymeleaf处理能力
     * @param model
     * @return
     */
    @RequestMapping(path = "test05",method = RequestMethod.GET)
    public String test05(Model model) {
        model.addAttribute("name","张三");
        return "test05";
    }

    /**
     * 用thymeleaf同时设置多属性的值
     * @param model
     * @return
     */
    @RequestMapping(path = "test06",method = RequestMethod.GET)
    public String test06(Model model) {
        model.addAttribute("name","张三");
        model.addAttribute("selected","true");
        model.addAttribute("un_selected","false");
        return "test06";
    }


}
